import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  const isCollapsed = ref(false)
  const userInfo = ref<any>(null)
  const token = ref<string | null>(localStorage.getItem('token'))

  // 设置侧边栏折叠状态
  const setCollapsed = (status: boolean) => {
    isCollapsed.value = status
  }

  // 设置用户信息
  const setUserInfo = (info: any) => {
    userInfo.value = info
  }

  // 设置token
  const setToken = (newToken: string | null) => {
    token.value = newToken
    if (newToken) {
      localStorage.setItem('token', newToken)
    } else {
      localStorage.removeItem('token')
    }
  }

  // 登出
  const logout = () => {
    token.value = null
    userInfo.value = null
    localStorage.removeItem('token')
  }

  return {
    isCollapsed,
    userInfo,
    token,
    setCollapsed,
    setUserInfo,
    setToken,
    logout
  }
}) 